package com.example.competionmanagebackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.competionmanagebackend.entity.Messages;
import com.example.competionmanagebackend.utils.PageRequest;
import com.example.competionmanagebackend.utils.PageResult;
import com.example.competionmanagebackend.utils.Result;
import com.example.competionmanagebackend.service.MessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/messages")
public class MessagesController {

    @Autowired
    private MessagesService messagesService;

    /**
     * 分页查询留言
     */
    @PostMapping("/getMessagesPage")
    public ResponseEntity<Result<PageResult<Messages>>> getMessagesPage(@RequestBody PageRequest pageRequest) {
        Page<Messages> page = messagesService.getMessagesPage(
                pageRequest.getCurrentPage(),
                pageRequest.getPageSize()
        );
        PageResult<Messages> result = new PageResult<>(page);
        return Result.success(result);
    }

    /**
     * 新增/编辑留言
     */
    @PostMapping("/edit")
    public ResponseEntity<Result<Messages>> editMessage(@RequestBody Messages message) {
        if (message.getId() == null) {
            // 新增
            messagesService.insertMessage(message);
        } else {
            // 编辑
            messagesService.updateMessage(message);
        }
        return Result.success(message);
    }

    /**
     * 删除留言
     */
    @GetMapping("/delete")
    public ResponseEntity<Result<String>> deleteMessage(@RequestParam("id") Integer id) {
        messagesService.deleteMessage(id);
        return Result.success("删除成功");
    }
}
